<?php

namespace app\common\model;

use think\Db;
use think\Model;


class Funds extends Model
{

    

    

    // 表名
    protected $name = 'funds';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
	protected $append = [
		'create_time_text',
		'type_text',
		'operation_type_text',
		'account_type_text'
	];
	
	const OPE_TYPE_PLUS = 1;
	const OPE_TYPE_LESS = 2;
	
	public static $operationTypeList = [
		self::OPE_TYPE_PLUS => '添加',
		self::OPE_TYPE_LESS => '减少'
	];
	
	
	const ACCOUNT_TYPE_BALANCE = 1;
	const ACCOUNT_TYPE_INTEGRAL = 2;
	
	public static $accountTypeList = [
		self::ACCOUNT_TYPE_BALANCE => '余额',
		self::ACCOUNT_TYPE_INTEGRAL => '积分',
	];
	
	
	
	const TYPE_WITHDRAW = 1;
	const TYPE_PAY_ORDER = 2;
	const TYPE_REBATE = 3;
	const TYPE_ADMIN_OPERATE = 4;
	const TYPE_WITHDRAE_BACK = 5;
	
	public static $typeList = [
		self::TYPE_WITHDRAW => '提现',
		self::TYPE_PAY_ORDER => '支付订单',
		self::TYPE_REBATE => '订单返利',
		self::TYPE_ADMIN_OPERATE => '后台操作',
		self::TYPE_WITHDRAE_BACK => '提现退款',
	];
	
	public function getCreateTimeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['create_time'];
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	
	public function getTypeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['type'];
		$list = self::$typeList;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	public function getOperationTypeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['operation_type'];
		$list = self::$operationTypeList;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	public function getAccountTypeTextAttr($value, $data)
	{
		$value = $value ? $value : $data['account_type'];
		$list = self::$accountTypeList;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	protected function setCreateTimeAttr($value)
	{
		return $value && !is_numeric($value) ? strtotime($value) : $value;
	}
	
	public function user(){
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function relationuser(){
		return $this->belongsTo('user', 'relation_user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	
	/**
	 * 添加流水记录
	 * @param $userId 用户ID
	 * @param $amount 金额
	 * @param float|int $beforeAmount 变动前金额
	 * @param $accountType 帐户类型
	 * @param $operationType 操作类型
	 * @param $type 类型
	 * @param string $orderId 订单ID
	 * @param string $remark 备注
	 * @param int $relation_user_id 相关用户ID
	 * @return bool
	 */
	public function addRecord($userId, $amount, $beforeAmount, $accountType, $operationType, $type, $orderId = '', $remark="", $relation_user_id=''){
		$data = [
			'user_id' => $userId,
			'amount' => $amount,
			'before' => $beforeAmount,
			'account_type' => $accountType,
			'operation_type' => $operationType,
			'type' => $type,
			'order_id' => $orderId,
			'remark' => $remark,
			'create_time' => time(),
			'relation_user_id' => $relation_user_id
		];
		$res = Db::name('funds')->insert($data);
		if($res){
			//添加信息
			return true;
		}
		return false;
	}




}
